Adding Active Server Pages |
What are Active Server Pages? |
Displaying Date, Time, and Text |
Using Counters, Variables, and Forms |
Displaying Server Statistics |
Active Server Pages Server-Side Scripting Programmer's Reference |
Counters are useful for keeping statistics such as how many "hits" each of your Web pages has received. To create a counter, use the counter.get and counter.increment functions. If either one of these is used but a counter doesn't exist yet, the counter is created.
To display the value of a counter in a Web page, type:
<% =counter.get(countername) %>
where countername is a string containing the name of the counter at the point where you want the counter's value to appear.
To increase the value of a counter by one and display the new value, type:
<% =counter.increment(countername) %>
where countername is the name of the counter.
To increase the value of the counter by one but not display it, omit the equal sign (=).
To add a "hit counter" to a Web page, type this:
There have been <% =counter.increment("hits") %> visits to this Web page.
The first visitor to the Web page will see this:
There have been 1 visits to this Web page.
After that, each time the server receives a new request for that Web page, it will increase the "hits" counter by one before it sends the page to the browser to be viewed.
Forms are a convenient way to communicate with visitors to your Web site. Using forms, you can create a survey form and ask visitors to fill it out. When they fill out the form, you can process the results automatically.
With forms, there are two steps: first you create the form, and then you process it. To create a form for an Active Server Page, just create a standard HTML form. For more information on creating standard HTML forms visit, http://www.microsoft.com/workshop/author/authoring-contents1.htm.
For processing HTML forms, you're probably used to writing CGI scripts in C or Perl. Active Server Pages provide a mechanism for processing forms that, unlike CGI scripting, doesn't involve serious programming: the request.form and tools.processform functions.
To display the contents of each field in the form, type:
<% =request.form(fieldname) %>
where fieldname is the name of the field.
You'll probably want to do more with your forms than display their contents in a Web page. For example, based on the contents of the form, you may want to increment various counters to gather statistical information about visitors to your Web site. To save the contents of a field for further processing, you need to create a variable. To do that, just make up a name and set it equal to the contents of the field.
For example, if you have a field called "Name" in your form, you can save it into a variable called "VisitorName" by typing:
<% VisitorName = request.form("Name") %>
The example below uses a variable called "colornumber."
The form in this example asks users to vote for their favorite color: red, blue, or green. To vote, the user selects one of three radio buttons and clicks "OK" to submit the form. When the form is received, the server responds with a new form and the total votes that have been received for each of the three colors.
To try out this example, create an ASP file and cut-and-paste the following text into it.
<html><head><title>Color Popularity Contest</title></head> <FORM ACTION="" METHOD=POST> Vote for Your Favorite Color: <P><INPUT TYPE="RADIO" NAME="CONTROL3" VALUE="0" CHECKED>Red <INPUT TYPE="RADIO" NAME="CONTROL3" VALUE="1">Green <INPUT TYPE="RADIO" NAME="CONTROL3" VALUE="2">Blue <P><INPUT TYPE="SUBMIT" VALUE="OK"><INPUT TYPE="RESET" VALUE="Reset"> </FORM> <P><% colornumber = request.form("CONTROL3") %> <% if colornumber = "1" or colornumber = "2" or colornumber = "0" then %> You voted for <% if colornumber = "1" then %> green.<% counter.increment("greencounter") %> <% else %> <% if colornumber = "2" then %> blue.<% counter.increment("bluecounter") %> <% else %> <% if colornumber = "0" then %> red.<% counter.increment("redcounter") %> <% end if %> <% end if %> <% end if %> <P>Current vote tally: <P>red: <% =counter.get("redcounter") %> <P>green: <% =counter.get("greencounter") %> <P>blue: <% =counter.get("bluecounter") %> <% end if %> <P><P>There have been <% =counter.increment("hits") %> visits to this Web page.<BR> </body></html>
The tools.processform function is designed for use when you want to do fairly elaborate form processing.
The syntax for tools.processform is as follows:
<% tools.processform (string1: string, string2: string, [string3: string]) %>
The tools.processform function processes the contents of a form that's been filled out by a visitor to your Web site.
This is a separate file that contains your request.form commands and the HTML commands for formatting the results of those commands. By convention, this file is given the ".process" extension, but this is not required.
In this file, whenever you use server-side scripts (including request.form commands), you need to enclose them in slightly different-looking opening and closing tags: "<%%" instead of "<%" and "%%>" instead of "%>." This is so the server uses these scripts to direct the processing of the form in the ASP file instead of just running the scripts by themselves.
A form's response page is the page that appears when the form's SUBMIT button is pressed. This page begins by calling the tools.processform function one or more times. The rest of the page can contain whatever you want the user to see after the form is submitted. After you've created your response page, make sure to edit the ASP file that contains the form and set the form's ACTION attribute to the URL of your response page.
The best way to learn how to do complex form processing using the tools.processform function, .process files, and response pages is to study the guestbook and message-creation files that come with your Personal Web Server.
Return to top |